package com.jeeplus.modules.common.pdf.component;

import com.itextpdf.text.*;
import java.util.List;
import java.util.Map;

import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.draw.DottedLineSeparator;
import com.itextpdf.text.pdf.draw.LineSeparator;
import com.jeeplus.modules.api.studentdormitory.entity.StudentDormitoryPdf;

/**
 * @author wang.yishu
 * @version 1.0
 * @className PdfReport
 * @description TODO
 * @date 2022/7/28 9:53
 */
public class PdfReport {
    // 定义全局的字体静态变量
    private static Font titlefont;
    private static Font headfont;
    private static Font keyfont;
    private static Font textfont;
    // 最大宽度
    private static int maxWidth = 520;
    // 静态代码块
    static {
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(bfChinese, 5, Font.NORMAL);
            headfont = new Font(bfChinese, 16, Font.NORMAL);
            keyfont = new Font(bfChinese, 12, Font.NORMAL);
            textfont = new Font(bfChinese, 12, Font.UNDEFINED);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 生成PDF文件
    public void generatePDF(Document document, List<StudentDormitoryPdf> list, Map<String, String> params) throws Exception {

        // 段落
        //Paragraph paragraph = new Paragraph("美好的一天从早起开始！", titlefont);
        Paragraph paragraph = new Paragraph("", titlefont);
        paragraph.setAlignment(1); //设置文字居中 0靠左   1，居中     2，靠右
        paragraph.setIndentationLeft(12); //设置左缩进
        paragraph.setIndentationRight(12); //设置右缩进
        paragraph.setFirstLineIndent(24); //设置首行缩进
        paragraph.setLeading(24f); //行间距
        paragraph.setSpacingBefore(5f); //设置段落上空白
        paragraph.setSpacingAfter(10f); //设置段落下空白
        // 表格

        for (int i = 0; i < list.size(); i++) {
            StudentDormitoryPdf studentDormitoryPdf = list.get(i);
            PdfPTable table = createTable(new float[] { 40, 120, 120, 120, 50, 150 });
            PdfPCell cell = createCell(params.get("sysYear") + "级新生寝室入住通知单", headfont, Element.ALIGN_CENTER, 6);
            cell.setMinimumHeight(30f);
            table.addCell(cell);
            table.addCell(createCell("学院", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(studentDormitoryPdf.getCollegeName(), textfont, Element.ALIGN_CENTER,2));
            table.addCell(createCell("专业", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(studentDormitoryPdf.getMajorName(), textfont, Element.ALIGN_CENTER,2));

            table.addCell(createCell("姓名", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(studentDormitoryPdf.getName(), textfont, Element.ALIGN_CENTER));
            table.addCell(createCell("性别", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(studentDormitoryPdf.getSex(), textfont, Element.ALIGN_CENTER));
            table.addCell(createCell("学号", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell(studentDormitoryPdf.getNumber(), textfont, Element.ALIGN_CENTER));

            String gradation = studentDormitoryPdf.getGradation();
            if ("1".equals(gradation)) {
                table.addCell(createCell("楼号", keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(studentDormitoryPdf.getApartmentName(), textfont, Element.ALIGN_CENTER));
                table.addCell(createCell("寝室号", keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(studentDormitoryPdf.getDormNumber(), textfont, Element.ALIGN_CENTER));
                table.addCell(createCell("床号", keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(studentDormitoryPdf.getBedNumber(), textfont, Element.ALIGN_CENTER));
            } else {
                table.addCell(createCell("楼号", keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(studentDormitoryPdf.getApartmentName(), textfont, Element.ALIGN_CENTER,2));
                table.addCell(createCell("寝室号", keyfont, Element.ALIGN_CENTER));
                table.addCell(createCell(studentDormitoryPdf.getDormNumber(), textfont, Element.ALIGN_CENTER,2));
            }

            table.addCell(createCell("备注", keyfont, Element.ALIGN_CENTER));
            table.addCell(createCell("", textfont, Element.ALIGN_CENTER,5));
            table.addCell(createCell("学院盖章        ", keyfont, Element.ALIGN_RIGHT, 6, false));
            table.addCell(createCell(params.get("currentTime"), textfont, Element.ALIGN_RIGHT, 6, false));
            table.addCell(createCell("", keyfont, Element.ALIGN_RIGHT, 6, false));
            Paragraph p1 = new Paragraph();
            p1.add(new Chunk(new LineSeparator()));
            document.add(paragraph);
            document.add(table);
            document.add(p1);
        }
        if (list.size() == 0) {
            Paragraph p = new Paragraph();
            p.add("无数据！");
            document.add(p);
        }

    }


/**------------------------创建表格单元格的方法start----------------------------*/
    /**
     * 创建单元格(指定字体)
     * @param value
     * @param font
     * @return
     */
    public PdfPCell createCell(String value, Font font) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(Element.ALIGN_CENTER);
        cell.setPhrase(new Phrase(value, font));
        cell.setMinimumHeight(24f);
        return cell;
    }
    /**
     * 创建单元格（指定字体、水平..）
     * @param value
     * @param font
     * @param align
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        cell.setMinimumHeight(24f);
        return cell;
    }
    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, int colspan) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setMinimumHeight(24f);
        return cell;
    }
    /**
     * 创建单元格（指定字体、水平居..、单元格跨x列合并、设置单元格内边距）
     * @param value
     * @param font
     * @param align
     * @param colspan
     * @param boderFlag
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, int colspan, boolean boderFlag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setColspan(colspan);
        cell.setPhrase(new Phrase(value, font));
        cell.setPadding(3.0f);
        cell.setMinimumHeight(24f);
        if (!boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(15.0f);
            cell.setPaddingBottom(8.0f);
        } else if (boderFlag) {
            cell.setBorder(0);
            cell.setPaddingTop(0.0f);
            cell.setPaddingBottom(15.0f);
        }
        return cell;
    }
    /**
     * 创建单元格（指定字体、水平..、边框宽度：0表示无边框、内边距）
     * @param value
     * @param font
     * @param align
     * @param borderWidth
     * @param paddingSize
     * @param flag
     * @return
     */
    public PdfPCell createCell(String value, Font font, int align, float[] borderWidth, float[] paddingSize, boolean flag) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        cell.setBorderWidthLeft(borderWidth[0]);
        cell.setBorderWidthRight(borderWidth[1]);
        cell.setBorderWidthTop(borderWidth[2]);
        cell.setBorderWidthBottom(borderWidth[3]);
        cell.setPaddingTop(paddingSize[0]);
        cell.setPaddingBottom(paddingSize[1]);
        cell.setMinimumHeight(24f);
        if (flag) {
            cell.setColspan(2);
        }
        return cell;
    }
/**------------------------创建表格单元格的方法end----------------------------*/


/**--------------------------创建表格的方法start------------------- ---------*/
    /**
     * 创建默认列宽，指定列数、水平(居中、右、左)的表格
     * @param colNumber
     * @param align
     * @return
     */
    public PdfPTable createTable(int colNumber, int align) {
        PdfPTable table = new PdfPTable(colNumber);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(align);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }
    /**
     * 创建指定列宽、列数的表格
     * @param widths
     * @return
     */
    public PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(maxWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }
    /**
     * 创建空白的表格
     * @return
     */
    public PdfPTable createBlankTable() {
        PdfPTable table = new PdfPTable(1);
        table.getDefaultCell().setBorder(0);
        table.addCell(createCell("", keyfont));
        table.setSpacingAfter(20.0f);
        table.setSpacingBefore(20.0f);
        return table;
    }
/**--------------------------创建表格的方法end------------------- ---------*/

}
